<?php
//作者千柒qq743395324
namespace Admin\Controller;

use Think\Controller;

class ClassDataController extends Controller
{
	public function _initialize(){
            //判断用户是否已经登录
        if (!isset($_SESSION['username'])) {
         redirect('Admin/Login');
         exit;
        }
    }
    public function index()
    {
        // $_SERVER['REQUEST_URI'];
        $data = M('classify')->order('id desc')->where(array('class_sid'=>0))->select();
        $this->assign('data',$data);

    	$this->display();
    }
    //分类添加
    public function class_post()
    {
        $class_name = I('post.class_name','','htmlspecialchars');
        $class_sid = I('post.class_sid','','htmlspecialchars');
        $class_ui = I('post.class_ui','','htmlspecialchars');
        $id = I('post.id','','htmlspecialchars');
//exit($class_name.$class_sid.$id);
        if(IS_AJAX){
            if(!$class_name){
                exit('请输入分类名称');
            }
        $sql = M('classify');

        $data['class_name'] = $class_name;
        //$data['class_sort'] = $class_sort;
            if($id){
                if($sql->where('id='.$id)->save($data)){
                    echo '操作成功';
                }else{
                    echo '操作失败';
                }
            }else{
            $data['class_sid'] = $class_sid;
            $data['add_time'] = strtotime(date("Y/m/d H:i:s"));
                if($sql->add($data)){
                echo "操作成功";
            }else{
                echo "非法错误";
            }
            }
        }
    }
    //删除
     public function class_del()
    {
         $id = I('post.id','','htmlspecialchars');
        if(M('classify')->where('class_sid='.$id)->find()){
            echo '该分类下还有子分类不可直接删除';
        }else{
            if(M('webdata')->where('sid='.$id)->find()){
                echo '该分类下还有网站不可直接删除';
            }else{
                if(M('webdata')->where('id='.$id)->delete()){
                    echo '操作成功';
                }else{
                    echo '操作失败';
                }
            }
        }
    }
}
